<?php

	class PlasaComponent extends Object {
		var $components = array(
			'Session'
		);
		
		var $controller;
		
		var $menus_for_layout = array();
		
		var $blocks_for_layout = array();
		
		var $products_for_layout = array();
		
		var $posts_for_layout = array();
		
		var $categories_for_layout = array();
		
		var $tags_for_layout = array();
		
		public function initialize(&$controller) {
			$this->controller =& $controller;
			
			$this->controller->domain = env('HTTP_HOST');
			
			$this->controller->shopInfo = $this->controller->Shop->find(
				'first',
				array(
					'conditions' => array(
						'domain' => $this->controller->domain
					),
					'fields' => array(
						'Shop.id',
						'Shop.alias',
						'Shop.theme',
						'Shop.name',
						'Shop.slogan',
						'Shop.keywords',
						'Shop.description',
						'Shop.email',
						'Shop.url',
						'Shop.logo',
						'Shop.logo_url'
					),
					'contain' => array(
						'Configuration' => array(
							'option',
							'value'
						),
						'Country' => array(
							'id',
							'isonum',
							'iso2',
							'iso3',
							'name'
						),
						'Currency' => array(
							'id',
							'name',
							'code',
							'symbol_left',
							'symbol_right',
							'decimal_places',
							'decimals',
							'thousands',
							'value'
						)
					),
					'recursive' => 1
				)
			);
			
			if ($this->controller->shopInfo) {
				# load components and helpers
				Configure::write('Shop', $this->controller->shopInfo['Shop']);
				Configure::write('Shop.Configuration', $this->controller->shopInfo['Configuration']);
				
				$this->controller->view = 'Theme';
				$this->controller->theme = 'default';
				$this->controller->theme = 'bootstrap';
				
				$this->menus();
				$this->categories();
				
				# load dashboard and authorize
				# if (!empty($this->params['url']) && preg_match('/^dashboard+/', $this->params['url']['url'])) {
				if ($this->controller->shopInfo['Shop']['theme']) {
					$this->controller->theme = $this->controller->shopInfo['Shop']['theme'];
				}
				
				if (preg_match('/^dashboard+/', $this->controller->params['action'])) {
					$this->controller->layout = 'dashboard';
					$this->controller->params['dashboard'] = true;
				}
			}
			else {
				$this->cakeError('shopNoExists', array('shopAlias' => $this->controller->domain));
			}
		}
		
		function products() {
			
		}
		
		function menus() {
			$menus = array('main');
			foreach ($menus AS $menuAlias) {
				$menu = $this->controller->Shop->Menu->find('first', array(
					'conditions' => array(
						'Menu.shop_id' => Configure::read('Shop.id'),
						'Menu.disabled' => 0,
						'Menu.alias' => $menuAlias,
						'Menu.link_count >' => 0,
					),
					'cache' => array(
						'name' => Configure::read('Shop.alias') . '_menu_' . $menuAlias,
						'config' => 'plasa_menus'
					),
					'recursive' => -1,
				));
				
				if (isset($menu['Menu']['id'])) {
					$this->menus_for_layout[$menuAlias] = array();
					$this->menus_for_layout[$menuAlias]['Menu'] = $menu['Menu'];
					$findOptions = array(
						'conditions' => array(
							'Link.menu_id' => $menu['Menu']['id'],
							'Link.disabled' => 0
						),
						'order' => array(
							'Link.lft' => 'ASC',
						),
						'cache' => array(
							'name' => Configure::read('Shop.alias') . '_menu_'.$menu['Menu']['id'].'_links',
							'config' => 'plasa_menus'
						),
						'recursive' => -1,
					);
					$links = $this->controller->Shop->Menu->Link->find('threaded', $findOptions);
					$this->menus_for_layout[$menuAlias]['threaded'] = $links;
				}
			}
			
			$this->controller->set('menus_for_layout', $this->menus_for_layout);
		}
		
		function categories($conditions = array()) {
			$conditions = array_merge(
				array(
					'Taxonomy.shop_id' => Configure::read('Shop.id'),
					'Taxonomy.taxonomy' => 'category'
				),
				$conditions
			);
			
			$this->categories_for_layout = $this->controller->Shop->Taxonomy->find(
				'threaded',
				array(
					'conditions' => $conditions,
					'contain' => array(
						'Term'
					),
					'recursive' => 0
				)
			);
			
			$this->controller->set('categories_for_layout', $this->categories_for_layout);
		}

		function tags($conditions = array()) {
			$conditions = array_merge(
				array(
					'Taxonomy.shop_id' => Configure::read('Shop.id'),
					'Taxonomy.deleted' => 0,
					'Taxonomy.taxonomy' => 'tags'
				),
				$conditions
			);
			
			$this->tags_for_layout = $this->controller->Shop->Taxonomy->find(
				'list',
				array(
					'conditions' => $conditions,
					'recursive' => -1
				)
			);
		}
	}